package com.amkai.aes.webservice.binary.objecthandler.diee;

import java.io.Serializable;

import org.apache.log4j.Logger;

import com.amkai.aes.aescommon.service.binary.job.Job;
import com.amkai.aes.server.diee.DIEEServerUtils;
import com.amkai.aes.webservice.binary.JobObjectHandler;

public abstract class AbstractLoggerHandler<E extends Job, InputObject extends Serializable, OutputObject extends Serializable> implements JobObjectHandler<E, InputObject, OutputObject> {

	private final Logger logger = DIEEServerUtils.getLogger();
	private final static String line = "+--------------------------------------------------------------------+";

	protected void line(String title, boolean start) {
		if (start) info(line);
		while (title.length()<line.length()-3) title = " " + title + " ";
		while (title.length()<line.length()-2) title += " ";
		info("|"+title+"|");
		if (!start) info(line);
	}
	
	protected String getArrayValueString(String[] values) {
		if (values==null) return "<empty>";
		StringBuilder str = new StringBuilder();
		for (String s : values) {
			if (str.length()>0) str.append("; ");
			str.append(s);
		}
		return str.toString();
	}
	
	protected String getArrayValueString(Object[] values) {
		if (values!=null) {
			String[] strValues = new String[values.length];
			for (int i=0; i<values.length; i++) {
				if (values[i]==null)
					strValues[i] = null;
				else
					strValues[i] = values[i].toString();
			}
			return getArrayValueString(strValues);
		}
		return null;
	}

	public void debug(Object message) {
		logger.debug(message);
	}

	public void error(Object message) {
		logger.error(message);
	}

	public void error(Object message, Throwable throwable) {
		logger.error(message, throwable);
	}
	
	public void fatal(Object message) {
		logger.fatal(message);
	}

	public void fatal(Object message, Throwable throwable) {
		logger.fatal(message, throwable);
	}

	public void info(Object message) {
		logger.info(message);
	}

	public void warn(Object message) {
		logger.warn(message);
	};
	
	public Logger getLogger() {
		return logger;
	}
}
